﻿using FourWayClimbingDemo.Chess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FourWayClimbingDemo
{
    /// <summary>
    /// 四路爬控制器
    /// </summary>
    public sealed class FwcPlay
    {
        private ChessBoard _chessBoard = null;
        private Referee _referee = null;
        private const int bannerTop = 15;
        private const int boardTop = 12;

        public void ShowChessBorad(){
            Console.SetCursorPosition(0, 0+ bannerTop);
            Console.WriteLine(_chessBoard.Render());
            Console.WriteLine("                                                                                     ");
            Console.WriteLine("                                                                                     ");
            Console.WriteLine("                                                                                     ");
            Console.WriteLine("                                                                                     ");
            Console.WriteLine("                                                                                     ");
            Console.WriteLine("                                                                                     ");
            Console.WriteLine("                                                                                     ");
            Console.WriteLine("                                                                                     ");
            Console.WriteLine("                                                                                     ");
            Console.SetCursorPosition(0, boardTop + bannerTop);
            Console.Write($"执[{_referee.CurrentPlayer.Tag}]棋子,请{_referee.CurrentPlayer.Name}落子：");                    
        }

        public void Init() {
            var player1 = new RealPlayer("小明");
            player1.InitChessPieces("O", true);
            var player2 = new AIPlayer("AI");
            player2.InitChessPieces("X", false);

            _chessBoard = new ChessBoard();
            _chessBoard.Init(player1.GetChessPieces(), player2.GetChessPieces());
            _referee = new Referee(_chessBoard, player1, player2);

            ShowChessBorad();
        }

        public void PlayChess(string posStr,Dir dir) {
            string pattern = "^[1-4]{1},[1-4]{1}$";
            if (!System.Text.RegularExpressions.Regex.IsMatch(posStr, pattern)) {
                ErrorInfo("请输入正确的棋子坐标");
                return;
            }
            string[] posArr = posStr.Trim().Split(",");
            var res = _referee.MoveChessPiece(Int32.Parse(posArr[0])-1, Int32.Parse(posArr[1]) - 1, dir);
            if (!res) {
                ErrorInfo("此处无棋子，或棋子移动违规");
                return;
            }
            ShowChessBorad();

            var winPlayer = _referee.GetWinner();
            if (winPlayer != null) {
                ErrorInfo($"{winPlayer.Name}获得本局胜利");
                return;
            }
        }

        public Boolean IsAutoPlay() {

            return _referee.CurrentPlayer.IsWantPlay;
        }

        public void PlayChess() {

            var pos = _referee.CurrentPlayer.IWantPlay(_chessBoard);
            if (pos == null) return;
            var res = _referee.MoveChessPiece(pos.Value.cp.GetPosition(), pos.Value.dir);
            if (!res)
            {
                ErrorInfo("此处无棋子，或棋子移动违规");
                return;
            }
            ShowChessBorad();

            var winPlayer = _referee.GetWinner();
            if (winPlayer != null)
            {
                ErrorInfo($"{winPlayer.Name}获得本局胜利");
                return;
            }

        }



        /// <summary>
        /// 重新开始
        /// </summary>
        public void ReStart() {
            _referee.ReStart();
            ShowChessBorad();
        }

        public void ErrorInfo(string msg) {
            Console.SetCursorPosition(0, boardTop -1 + bannerTop);
            Console.WriteLine("                                                                                     ");
            Console.SetCursorPosition(0, boardTop -1 + bannerTop);
            Console.WriteLine(msg);
            Console.SetCursorPosition(22, boardTop + bannerTop);
            Console.Write("                                            ");
            Console.SetCursorPosition(22, boardTop + bannerTop);
        }
    }



 
}
